<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Customer Point
 * @version 	1.0 (March 2012)
 * @author 		xuanhoapro <xuanhoapro@gmail.com>
 * @copyright   Copyright (c) 2011 ZPROFESS. (http://www.zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

class ZcustomerPointObject extends ObjectModel {
	protected 	$table ='zcustomerpoint_accounts';
	protected 	$identifier = 'id_zcustomerpoint_accounts';
	
    public      $id_zcustomerpoint_accounts;
    public 		$points_current;
    public 		$id_customer_parent;
    public 		$username;
    public 		$name;
    public      $block = false;
	
	protected $fieldsValidate = array(
		'id_zcustomerpoint_accounts' 	 => 'isInt',
		'points_current' 				 => 'isInt',
		'id_customer_parent' 			 => 'isInt',
		'block' 		 				 => 'isInt',
	);

	public function  __construct($id = null, $lang = null)
	{
		parent::__construct($id, $lang);
	}

	public function getFields()
	{
		parent::validateFields();
		$fields['id_zcustomerpoint_accounts'] = intval($this->id_zcustomerpoint_accounts);
		$fields['points_current'] = intval($this->points_current);
		$fields['id_customer_parent'] = pSQL($this->id_customer_parent);
		$fields['username'] = pSQL($this->username);
		$fields['name'] = pSQL($this->name);
		$fields['block'] = intval($this->block);
		return $fields;
	}
	/**
	 * 
	 * change status block of point customer
	 * 
	 * @return bool
	 */
	public static function toggleBlock()
	{
		$return = Db::getInstance()->Execute('
			UPDATE `'.pSQL(_DB_PREFIX_.'zcustomerpoint_accounts').'`
			SET `block` = !`block`
			WHERE `id_zcustomerpoint_accounts` = '.Tools::getValue('id_zcustomerpoint_accounts'));
		if($return)			
			return true;
		else
			return false;
	}
	
	/**
	 * 
	 * change block status
	 * @param (int)$block			status block 
	 * @param (int)$id_customer		id customer update block 
	 * 
	 * @return bool
	 */
	public static function _changeBlock($block, $id_customer){
		return Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'zcustomerpoint_accounts` 
										SET block = '.$block.' 
										WHERE `id_customer` = '.(int)($id_customer));
	}
	/**
	 * 
	 * Add points when registering
	 * @param (int)$id_customer			id customer
	 * @param (int)$id_customer_parent	id customer parent
	 * @param (int)$points	 			points
	 * @param (text)$name	 	 	first name
	 * @param (text)$username			username
	 * 
	 * @return bool
	 */
	public static function _addPointRegistering($id_customer_parent, $points, $name, $username){
		global $cookie;
		$field = array('id_customer' => (int)$cookie->id_customer, 
						'points_current' => $points,
						'username' => $username,
						'name' => $name,
						'id_customer_parent'=>(int)$id_customer_parent,
		);
		return Db::getInstance()->AutoExecute(_DB_PREFIX_.'zcustomerpoint_accounts', $field, 'INSERT');
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param (int)$id_customer		id customer
	 * @param (int)$points			points
	 * @param (bool)$add			add(true) or subtract(false)
	 * 
	 * @return bool
	 */
	public static function _updatePointToCustomer($id_customer, $points, $add = true){
		return DB::getInstance()->Execute('UPDATE '._DB_PREFIX_.'zcustomerpoint_accounts 
											SET `points_current` = points_current'.($add ? '+' : '-').$points.'
											WHERE `id_customer` ='.(int)$id_customer);
	}
	
	/**
	 * 
	 * Add points by time online
	 * @param (int)$timeDelay(minutes)		time delay
	 * @param (int)$timesAdd(minutes)		time to add points
	 * @param (int)$points					points add by timeAdd
	 */
	public static function _addPointByTimes($timeDelay, $timesAdd, $points){
		global $cookie;
		$time_now = date('Y-m-d H:i:s');
		
		$cookie->end_track_time = $time_now;
		$timeCheckDelay = strtotime($cookie->end_track_time) - strtotime($cookie->start_track_time_tmp);
		// onl 20 min
		if ($timeCheckDelay <= $timeDelay*60){
			$cookie->start_track_time_tmp = $time_now;
			$timeStatic = strtotime($cookie->end_track_time) - strtotime($cookie->start_track_time);
			// onl 1 hours
			if ($timeStatic >= $timesAdd*60){
				// add point by time(time online 1 hours)
				DB::getInstance()->Execute('UPDATE '._DB_PREFIX_.'zcustomerpoint_accounts 
										SET `points_current` = points_current+'.$points.'
										WHERE `id_customer` ='.(int)$cookie->id_customer);
				// update  timeline
				$cookie->start_track_time_tmp = $time_now;
				$timeNew = strtotime($time_now) - (strtotime($timeStatic) - $timesAdd*60);
				$cookie->start_track_time = date('Y-m-d H:i:s', $timeNew);
			}
		}else {
			$cookie->start_track_time_tmp = $time_now;
			$cookie->start_track_time = $cookie->start_track_time_tmp;
		}
	}
	
	/**
	 * 
	 * check points when like or comment
	 * @param $id_customer
	 * @param $action like(true) or comment(false)
	 */
	public static function _checkPointsCustomer($id_customer,$action = false){
		global $cookie;
		$pointsLike = Configuration::get('ZPOINTS_LIKE');
		$pointsComment = Configuration::get('ZPOINTS_COMMENT');
		$customerPoint = Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'zcustomerpoint_accounts 
														WHERE id_customer = '.(int)$id_customer);
		if ($customerPoint['points_current'] > 0){
			if ($action===true AND $customerPoint['points_current'] >= $pointsLike){
				return true;
			} elseif ($action===false AND $customerPoint['points_current'] >= $pointsComment){
				return true;
			}else 
				return false;
		}else 
			return false;
		
	}
	
}
	
	